import os
import time
import unittest
import requests
from HtmlTestRunner import HTMLTestRunner

# 获取验证码： http://localhost/index.php?m=Home&c=User&a=verify
# 登录     ： http://localhost/index.php?m=Home&c=User&a=do_login
"""
导包
创建测试类

创建测试方法 
setup
实例化session对象 
定义验证接口url地址 
定义正如接口url地址

teardown
关闭session对象

登录成功
发送验证码请求并断言 
发登录请求并断言

账号不存在
发送验证码请求并断言 
发登录请求并断言

密码错误
发送验证码请求并断言 
发登录请求并断言
"""

# 创建测试类
class TPshopLogin(unittest.TestCase):
    # unittest框架提供的特殊方法，用于测试用例的生命周期管理
    # setup用于预置测试环境:在每个测试方法(如test_login)开始前自动执行
    # 初始化资源:创建测试所需的依赖对象或状态(如会话，连接，数据)
    # 避免在每个测试方法中重复编写这些代码，确保每个测试方法都从一个干净的会话开始
    def setUp(self): # 脚手架
        # 创建一个会话对象(保持cookies)
        self.session = requests.Session()
        # 定义验证接口url地址
        self.url_verify = "http://localhost/index.php?m=Home&c=User&a=verify"
        # 定义登录接口url地址
        self.url_login = "http://localhost/index.php?m=Home&c=User&a=login"

    # unittest框架提供的特殊方法，用于测试用例的生命周期管理
    # 清理测试环境:在每个测试方法执行完毕后无论测试成功/失败都自动执行
    # 释放资源:关闭连接，清理临时数据，防止资源泄露(如未关闭的话会占用端口)，避免后续测试被残留影响
    # teardown
    def tearDown(self): # 脚手架
        self.session.close()

    # case001登陆成功
    def test01_login_success(self):
        # 获取验证码
        response = self.session.get(self.url_verify)
        # 判断响应码是否为200
        self.assertEqual(response.status_code, 200)
        # 当访问验证码接口时，服务器通常会返回一张图片(如PNG/JPEG)，并通过响应头的Content-Type字段声明类型(例如image/png)
        # 此断言确保接口返回的是图片而非文本或其他格式(防止接口错误返回JSON或HTML)，避免后续登陆步骤因验证码格式错误而失败
        self.assertIn("Image", response.headers.get("Content-Type"))
        # assertIn(a, b)检查a是否在b中
        # assertEqual(a, b)检查a == b
        # assertTrue(x) 检查x为True

        # 发送登录请求并断言
        login_data = {"username": "admin",
                      "password": "admin",
                      "verify_code": "8888"
                      }
        response = self.session.post(self.url_login, data=login_data)
        print(response.json())
        self.assertEqual(response.status_code, 200)
        self.assertEqual(1, response.json().get("status"))
        self.assertIn("登陆成功", response.json().get("message"))

    # case002账号不存在
    def test02_login_user_is_not_exist(self):
        response = self.session.get(self.url_verify)
        self.assertEqual(response.status_code, 200)
        self.assertIn("Image", response.headers.get("Content-Type"))

        # 发送登录请求并断言
        login_data = {"username": "13488888899",
            "password": "123456",
            "verify_code": "8888"}
        response = self.session.post(self.url_login, data=login_data)
        print(response.json())
        self.assertEqual(response.status_code, 200)
        self.assertEqual(-1, response.json().get("status"))
        self.assertIn("账号不存在", response.json().get("message"))

    # case003密码错误
    def test03_login_password_error(self):
        response = self.session.get(self.url_verify)
        self.assertEqual(response.status_code, 200)
        self.assertIn("Image", response.headers.get("Content-Type"))

        login_data = {"username": "13488888888",
            "password": "error",
            "verify_code": "8888"}

        response = self.session.post(self.url_login, data=login_data)
        print(response.json())
        self.assertEqual(response.status_code, 200)
        self.assertEqual(-2, response.json().get("status"))
        self.assertIn("密码错误",response.json().get("message"))
    # case004 不输入手机号
    def test04_login_without_mobile(self):
        response = self.session.get(self.url_verify)
        self.assertEqual(response.status_code, 200)
        self.assertIn("Image", response.headers.get("Content-Type"))

        login_data = {"username": "",
                      "password": "error",
                      "verify_code": "8888"}

        response = self.session.post(self.url_login, data=login_data)
        print(response.json())
        self.assertEqual(200, response.status_code)
        self.assertEqual(False, response.json().get("success"))
        self.assertEqual(20001, response.json().get("code"))
        self.assertIn("用户名或密码错误", response.json().get("message"))
    # case005 登录不输入密码
    def test05_login_without_passsword(self):
        response = self.session.get(self.url_verify)
        self.assertEqual(response.status_code, 200)
        self.assertIn("Image", response.headers.get("Content-Type"))

        login_data = {"username": "13800000002",
                      "password": "",
                      "verify_code": "8888"}
        response = self.session.post(self.url_login, data=login_data)
        print(response.json())
        self.assertEqual(200, response.status_code)
        self.assertEqual(False, response.json().get("success"))
        self.assertEqual(20001, response.json().get("code"))
        self.assertIn("用户名或密码错误", response.json().get("message"))

    # case006 输入手机号超出11位
    def test06_login_mobile_max_length_validation(self):
        response = self.session.get(self.url_verify)
        self.assertEqual(response.status_code, 200)
        self.assertIn("Image", response.headers.get("Content-Type"))

        login_data = {"username": "138000000024373735846994",
                      "password": "123456",
                      "verify_code": "8888"}

        response = self.session.post(self.url_login, data=login_data)
        print(response.json())
        self.assertEqual(200, response.status_code)
        self.assertEqual(False, response.json().get("success"))
        self.assertEqual(20001, response.json().get("code"))
        self.assertIn("用户名或密码错误", response.json().get("message"))

    # case007 登陆手机号未注册
    def test07_login_unregistered_mobile(self):
        response = self.session.get(self.url_verify)
        self.assertEqual(response.status_code, 200)
        self.assertIn("Image", response.headers.get("Content-Type"))

        login_data = {"username": "13800000009",
                      "password": "123456",
                      "verify_code": "8888"}

        response = self.session.post(self.url_login, data=login_data)
        print(response.json())
        self.assertEqual(200, response.status_code)
        self.assertEqual(False, response.json().get("success"))
        self.assertEqual(20001, response.json().get("code"))
        self.assertIn("用户名或密码错误", response.json().get("message"))

    # case008 登录手机号不是数字
    def test08_login_non_digit_mobile(self):
        response = self.session.get(self.url_verify)
        self.assertEqual(response.status_code, 200)
        self.assertIn("Image", response.headers.get("Content-Type"))

        login_data = {"username": "error",
                      "password": "123456",
                      "verify_code": "8888"}

        response = self.session.post(self.url_login, data=login_data)
        print(response.json())
        self.assertEqual(200, response.status_code)
        self.assertEqual(False, response.json().get("success"))
        self.assertEqual(20001, response.json().get("code"))
        self.assertIn("用户名或密码错误", response.json().get("message"))

    # case009登录手机号长度小于11位
    def test09_login_mobile_length_less_than_11(self):
        response = self.session.get(self.url_verify)
        self.assertEqual(response.status_code, 200)
        self.assertIn("Image", response.headers.get("Content-Type"))

        login_data = {"username": "123",
                      "password": "123456",
                      "verify_code": "8888"}

        response = self.session.post(self.url_login, data=login_data)
        print(response.json())
        self.assertEqual(200, response.status_code)
        self.assertEqual(False, response.json().get("success"))
        self.assertEqual(20001, response.json().get("code"))
        self.assertIn("用户名或密码错误", response.json().get("message"))




